module service.focus_service;

import d2sqlite3;
import model.article;
import utils.const_val;

/** 
 * 获取3条聚焦文章列表
 * Returns: Article[]
 */
Article[] getFocusList ()
{
    Database db = Database(CONN_STR);
    ResultRange results = db.execute(
        `SELECT * FROM article WHERE isHot="Y" OR isTop="Y" ORDER BY viewsCount + commentsCount DESC LIMIT 3;`);

    Article[] articleList = [];
    if (results.empty)
    {
        return articleList;
    }
    foreach (Row row; results)
    {
        auto id = row["id"].as!int;
        auto title = row["title"].as!string;
        auto thumbnail = row["thumbnail"].as!string;
        auto summary = row["summary"].as!string;
        auto isHot = row["isHot"].as!string;
        auto isTop = row["isTop"].as!string;
        auto visible = row["visible"].as!string;
        auto categoryId = row["categoryId"].as!int;
        auto categoryTitle = row["categoryTitle"].as!string;
        auto viewsCount = row["viewsCount"].as!int;
        auto commentsCount = row["commentsCount"].as!int;
        auto createTime = row["createTime"].as!string;
        auto updateTime = row["updateTime"].as!string;

        Article article = {
            id: id, title: title, thumbnail: thumbnail, summary: summary, isHot: isHot, isTop: isTop,
            visible: visible, categoryId: categoryId, categoryTitle: categoryTitle, viewsCount: viewsCount,
            commentsCount: commentsCount, createTime: createTime, updateTime: updateTime
        };
        articleList ~= article;
    }

    return articleList;
}
